﻿Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Public Class frmCarrera

    Private contador As Integer = 0
    Private vida As Integer = 50
    Private puntos As Integer = 1
    Private coefScroll As Integer = 1

    Private Sub frmCarrera_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            Dim fichero As New FileStream("Records.txt", FileMode.Open)
            Dim binary As New BinaryFormatter()
            records = CType(binary.Deserialize(fichero), Dictionary(Of String, Integer))
            fichero.Close()
        Catch ex As Exception
        End Try

        lblVida.Text = vida

        btnComenzar.Enabled = False
        btnParar.Enabled = False
        btnIniciar.Enabled = False

    End Sub
    Private Sub frmCarrera_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Dim fichero As New FileStream("Records.txt", FileMode.Create)
        Dim binary As New BinaryFormatter()
        binary.Serialize(fichero, records)
        fichero.Close()

        Dim mensajeRecords As String = ""
        For i As Integer = 0 To records.Count - 1
            mensajeRecords = mensajeRecords & records.Keys(i) & ": " & records(records.Keys(i)) & " || "
        Next

        MessageBox.Show(mensajeRecords, "RECORDS", MessageBoxButtons.OK, MessageBoxIcon.None)



    End Sub

    Private Sub btnComenzar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComenzar.Click

        btnComenzar.Enabled = False
        btnParar.Enabled = True
        btnIniciar.Enabled = False
        cmbCorredores.Enabled = False

        timSemaforo.Start()
        timJugadores.Start()

    End Sub

    Private Sub timSemaforo_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timSemaforo.Tick

        contador = contador + 1

        If contador < 8 Then
            radVerde.Checked = True
            radRojo.ForeColor = Color.Black
            radVerde.ForeColor = Color.Green
        Else
            If contador < 10 Then
                radAmbar.Checked = True
                radVerde.ForeColor = Color.Black
                radAmbar.ForeColor = Color.Orange
            Else
                radRojo.Checked = True
                radAmbar.ForeColor = Color.Black
                radRojo.ForeColor = Color.Red
            End If
        End If

        If contador = 15 Then
            contador = 0
        End If

        If contador = 0 Then
            timJugadores.Interval = 50
        ElseIf contador = 8 Then
            timJugadores.Interval = 100
        ElseIf contador = 10 Then
            timJugadores.Interval = 500
            If Not scrollVelocidad.Value > 90 Then
                scrollVelocidad.Value = scrollVelocidad.Value + 10
            End If
        End If

    End Sub

    Private Sub btnParar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParar.Click

        vida = vida / 2
        lblVida.Text = vida

        timSemaforo.Stop()
        timJugadores.Stop()

        btnComenzar.Enabled = True
        btnParar.Enabled = False
        btnIniciar.Enabled = True
        cmbCorredores.Enabled = False

    End Sub

    Private Sub timJugadores_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timJugadores.Tick

        If (Integer.Parse(lblVida.Text) <= 0) Then
            timSemaforo.Stop()
            timJugadores.Stop()
            btnComenzar.Enabled = False
            btnParar.Enabled = False
            btnIniciar.Enabled = True

            If records(cmbCorredores.Text) < Integer.Parse(lblPuntos.Text) Then
                records(cmbCorredores.Text) = Integer.Parse(lblPuntos.Text)
            End If

        End If

        lblPuntos.Text = puntos
        puntos = puntos + 1


        For i As Integer = 0 To corredores.Count - 1

            Dim botonCorredor As Corredor = corredores(i).Tag

            If botonCorredor.direccion Then

                corredores(i).Left = corredores(i).Left + (botonCorredor.velocidad + coefScroll)

                If corredores(i).Left + corredores(i).Width > Me.Width - 30 Then
                    botonCorredor.direccion = False
                    vida = vida - 1
                    lblVida.Text = vida
                End If
            Else

                corredores(i).Left = corredores(i).Left - (botonCorredor.velocidad + coefScroll)

                If corredores(i).Left < 15 Then
                    botonCorredor.direccion = True
                    vida = vida - 1
                    lblVida.Text = vida
                End If
            End If

        Next

    End Sub


    Private Sub scrollVelocidad_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles scrollVelocidad.ValueChanged

        Select Case scrollVelocidad.Value
            Case 1 To 20
                coefScroll = 2
            Case 21 To 40
                coefScroll = 4
            Case 41 To 60
                coefScroll = 6
            Case 61 To 80
                coefScroll = 8
            Case 81 To 100
                coefScroll = 10
        End Select

    End Sub

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim botonCorredor As Button = sender
        Dim propiedadesBoton As Corredor = botonCorredor.Tag

        propiedadesBoton.direccion = Not propiedadesBoton.direccion

        botonCorredor.Tag = propiedadesBoton

    End Sub


    Private Sub btnIniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIniciar.Click

        vida = 50
        lblVida.Text = vida
        puntos = 1
        cmbCorredores.Enabled = True
        cmbCorredores.SelectedIndex = -1

    End Sub

    Private Sub cmbCorredores_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCorredores.TextChanged


        For i As Integer = 0 To corredores.Count - 1
            Me.Controls.RemoveByKey(corredores(i).Name)
        Next

        corredores.Clear()

        If cmbCorredores.Text.Length = 0 Then
            btnComenzar.Enabled = False
            btnParar.Enabled = False
        Else
            btnComenzar.Enabled = True
            btnIniciar.Enabled = False

            Dim posicionY As Integer = 180

            For i As Integer = 0 To cmbCorredores.SelectedIndex + 2

                Dim boton As New Button

                'btnJugador

                boton.BackColor = System.Drawing.Color.PaleGoldenrod
                boton.Font = New System.Drawing.Font("Comic Sans MS", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                boton.Location = New System.Drawing.Point(16, posicionY)
                boton.Name = "btnCorredor" & (i + 1)
                boton.Size = New System.Drawing.Size(100, 40)
                boton.Text = "Corredor " & (i + 1)
                boton.Tag = New Corredor()

                posicionY = posicionY + 45
                Height = 450 + 45 * cmbCorredores.SelectedIndex
                btnComenzar.Top = 338 + 45 * cmbCorredores.SelectedIndex
                btnParar.Top = 338 + 45 * cmbCorredores.SelectedIndex
                btnIniciar.Top = 338 + 45 * cmbCorredores.SelectedIndex

                AddHandler boton.Click, AddressOf btn_Click

                Me.Controls.Add(boton)
                corredores.Add(boton)

            Next

        End If

    End Sub
End Class
